<template>
    <div>
        <div class="title">轮播图添加</div>
        <div id="head">
            <el-upload class="avatar-uploader" action="http://localhost:4000/admin/upload_temp" :show-file-list="false"
                :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="tempUrl" :src="tempUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </div>
        <el-button class="btn" @click="addSwiper">添加</el-button>
    </div>
</template>

<script>
import { _addSwiper } from '@/api/banners'
export default {
    name: 'BannerAdd',
    data() {
        return {
            imageUrl: '',
            tempUrl:""
        }
    },
    mounted() {
    },
    methods: {
        //上传成功
        handleAvatarSuccess(res, file) {
          
            // this.imageUrl = URL.createObjectURL(file.raw);
            let img=res.data.filename;
            this.tempUrl=`http://localhost:4000/temp/${img}`
            img=`http://localhost:4000/images/${img}`;
            this.imageUrl=img;
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg'||file.type === 'image/jpg' ||file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 5;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        async addSwiper() {
            // let img=this.imageUrl
            // img=img.replace("blob:","")
            const res = await _addSwiper({
                image_src: this.imageUrl,
                goods_id: "601fa4a9882000007800672c",
                navigator_url: "/pages/goods_detail/index?goods_id=38",
                open_type: "navigate"
            })
            if(res.code==200){
                this.$message.success("上传成功");
            }
            
        }
    },
}
</script>
<style scoped>
.title {
    font-size: 35px;
    margin-top: 20px;
    margin-left: 20px;
    font-weight: 600;
}

#head /deep/ .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-left: 20px;
    margin-top: 30px;
}

#head /deep/ .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

#head /deep/ .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 278px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

#head /deep/ .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.btn {
    margin-left: 120px;
    margin-top: 30px;
}
</style>